<?php
namespace Models;
use Resources;

class FormValidationTbdos extends Resources\Validation {
    
    public $checkNIDN = true;

	public function __construct()
    {
        parent::__construct();
        
        $this->session      = new Resources\Session;
        $this->request      = new Resources\Request;
        $this->datatables   = new Datatables;
    }
	
    public function setRules($rules= array())
    {
        $default = array(
            'NIDNNTBDOS' => array(
                'rules' => array(
                    'required',
                    'min' => 3,
					'numeric',
					'callback' => 'nidnExists'
                ),
                'label' => 'NIDN DOSEN',
                'filter' => array('trim', 'strtolower', 'ucwords')
            ),
			'NOKTPTBDOS' => array(
                'rules' => array(
                    'required',
                    'min' => 3,
                ),
                'label' => 'NO KTP',
                'filter' => array('trim', 'strtoupper', 'ucwords')
            ),
			'NMDOSTBDOS' => array(
                'rules' => array(
                    'required',
                    'min' => 3,
                ),
                'label' => 'NAMA DOSEN',
                'filter' => array('trim', 'strtoupper', 'ucwords')
            ),
			'TPLHRTBDOS' => array(
                'rules' => array(
                    'required',
                    'min' => 3,
                ),
                'label' => 'Tmpt Lahir',
                'filter' => array('trim', 'strtoupper', 'ucwords')
            ),
			'TGLHRTBDOS' => array(
                'rules' => array(
                    'required',
                    'min' => 3,
                ),
                'label' => 'Tgl Lahir',
                'filter' => array('trim', 'strtoupper', 'ucwords')
            ),
            'username' => array(
                'rules' => array(
                    'required',
                    'min' => 3,
                    'max' => 10,
                    'regex' => '/^([-a-z0-9_-])+$/i',
                    'callback' => 'usernameExists'
                ),
                'label' => 'Username',
                'filter' => array('trim', 'strtolower')
            ),
            'email' => array(
                'rules' => array(
                    'required',
                    'min' => 3,
                    'email',
                    'callback' => 'emailExists'
                ),
                'label' => 'Email',
                'filter' => array('trim', 'strtolower')
            ),
            'password' => array(
                'rules' => array(
                    'required',
                    'min' => 5,
                    'compare' => 'repassword'
                ),
                'label' => 'Password'
            ),
            'repassword' => array(
                'rules' => array(
                    'required'
                ),
                'label' => 'Retype Password'
            ),
            'photo' => array(
                'rules' => array(
                    'file'
                ),
                'label' => 'Profile Pic'
            )
        );
		
		$rules = array_merge($default, $rules);
		return $rules;
    }
    
	public function nidnExists($field, $value, $label)
    {
	    if( ! $this->checkNIDN )
            return true;

        if( ! $nidn = $this->datatables->getOne( array('NIDNNTBDOS' => $value) ) )
            return true;
        
        $this->setErrorMessage($field, 'NIDN already exists.');
            
        return false;
    }
	
	public function usernameExists($field, $value, $label)
    {
        if( $value != 'admin' )
            return true;
        
        $this->setErrorMessage($field, 'Username already exists.');
        
        return false;
    }
	   
    public function emailExists($field, $value, $label)
    {
        if( $value != 'admin@site.com' )
            return true;
        
        $this->setErrorMessage($field, 'Email already exists.');
        
        return false;
    }
}